#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

#define LEFT 	30000000
#define RIGHT 	30000200
#define THRNUM 	(RIGHT - LEFT + 1)

//参数结构体
struct thr_arg_st
{
	int n;
};
//线程处理函数
static void * the_primer(void * p)
{
	int i = ((struct thr_arg_st *)p)->n;
	int j;
	int mark = 1;
	for(j = 2; j < i / 2; j ++)
	{
		if(i % j == 0)
			{
				mark = 0;
				break;
			}
	}
	if(mark == 1)
		printf("%d is a primer\n", i);
	pthread_exit(p);
}

int main()
{
	int i, err;
	pthread_t tid[THRNUM];
	struct thr_arg_st * p;
	void * ptr_to_free;
	//循环20次
	for(i = LEFT; i <= RIGHT; i++)
	{
		//为参数申请空间
		p = malloc(sizeof(*p));
		if(p == NULL)
		{
			perror("malloc");
			exit(1);
		}
		//为参数赋值
		p->n = i;
		//创建线程
		err = pthread_create(&tid[i - LEFT], NULL, the_primer, p);
		if(err)
		{
			fprintf(stderr, "create:%s", strerror(err));
			exit(0);
		}
	}
	for(i = LEFT; i <= RIGHT; i++)
	{
		err = pthread_join(tid[i - LEFT], &ptr_to_free);
		free(ptr_to_free);
	}
	exit(0);
}